package ch.il07.converter.business.impl;

import ch.il07.converter.business.UserBusiness;
import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.PersistenceHelper;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.entities.User;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.utils.logging.Logger;
import ch.il07.utils.logging.LoggerHelper;

public class UserBusinessImpl implements UserBusiness {
	
	private final static Logger LOGGER = LoggerHelper.getLogger();
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean changePassword(String username, String oldPassword, String newPassword) {
		if(!ServiceHelper.parameterListNotNull(username, oldPassword, newPassword)) {
			throw new IllegalArgumentException("You HAVE TO give in username, oldPassword AND newPassword!");
		}
		
		try {
			Persistence persistence = PersistenceHelper.getPersistence();
			// Controlls if oldPassword is correct
			User correctUser = persistence.getUser(username, oldPassword);
			if(correctUser == null) {
				LOGGER.error("user with username " + username + " and some password doesn't exist");
				return false;
			}
			String email = correctUser.getEmail();
			User u = new User(username, newPassword, email);
			// Update user
			persistence.updateUser(u);
			return true;
		} catch (PersistenceException e) {
			LOGGER.error("problem while updateUser", e);
			return false;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean login(String username, String password) {
		if(!ServiceHelper.parameterListNotNull(username, password)) {
			throw new IllegalArgumentException("You HAVE TO give in username AND password!");
		}
		
		User u = null;
		try {
			Persistence persistence = PersistenceHelper.getPersistence();
			// Load user
			u = persistence.getUser(username, password);
			// User found and passwort correct?
			return u != null;
		} catch (PersistenceException e) {
			LOGGER.error("error while getting user from persistence", e);
			return false;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean register(String username, String password, String email, Integer accessLevel) {
		if(!ServiceHelper.parameterListNotNull(username, password, email, accessLevel)) {
			throw new IllegalArgumentException("You HAVE TO give in username, password, email AND accessLevel!");
		}
		
		// Get the specified accesslevel
		AccessLevel userAccessLevel = AccessLevel.getAccessLevelById(accessLevel);
		if(userAccessLevel == null) {
			// AccessLevel does not exist
			throw new IllegalArgumentException("You have specified an incorrect accessLevel for this user!");
		}
		try {
			Persistence persistence = PersistenceHelper.getPersistence();
			// Add the new user
			persistence.addUser(username, email, password, userAccessLevel);
			return true;
		} catch(PersistenceException e) {
			LOGGER.error("error while register", e);
			return false;
		}
	}
	
	@Override
	public boolean deleteUser(String username, String password) {
		try {
			Persistence perstistence = PersistenceHelper.getPersistence();
			perstistence.deleteUser(perstistence.getUser(username, password));
			return true;
		} catch (PersistenceException e) {
			LOGGER.error("error while deleteUser", e);
			return false;
		}
	}

	@Override
	public boolean isAdministrator(String username, String password) {
		try {
			Persistence persistence = PersistenceHelper.getPersistence();
			User u = persistence.getUser(username, password);
			return u.getAccessLevel() == AccessLevel.ADMINISTRATOR;
		} catch (PersistenceException e) {
			LOGGER.error("error while getting user", e);
			return false;
		}
	}
}